package com.hyc.gchatsystem.service.Impl;

import com.hyc.gchatsystem.mapper.LocalData;
import com.hyc.gchatsystem.medel.GroupModel;
import com.hyc.gchatsystem.medel.ReqModel;
import com.hyc.gchatsystem.medel.RespModel;
import com.hyc.gchatsystem.medel.UserModel;
import com.hyc.gchatsystem.service.ChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: gchatsystem
 * @package: com.hyc.gchatsystem.service.Impl
 * @className: ChatServiceImpl
 * @author: 冷环渊 doomwatcher
 * @description: TODO
 * @date: 2022/4/21 17:36
 * @version: 1.0
 */
@Component
@Slf4j
public class ChatServiceImpl implements ChatService {
    @Override
    public void addUser(ReqModel reqModel, RespModel respModel) {
        respModel.setMsg(reqModel.getNickname() + " : 加入聊天室");
        UserModel userModel = new UserModel(reqModel.getUid(), reqModel.getNickname(), 1);
        LocalData.userlist.add(userModel);
        respModel.setUsers(LocalData.userlist);
        respModel.setGroups(LocalData.grouplist);
    }

    @Override
    public void delUser(ReqModel reqModel, RespModel respModel) {
        respModel.setMsg(reqModel.getNickname() + " : 离开聊天室");
        UserModel userModel = null;
        //遍历在线用户的列表
        for (int i = 0; i < LocalData.userlist.size(); i++) {
            UserModel temp = LocalData.userlist.get(i);
            if (temp.getUid().equals(reqModel.getUid())) {
                userModel = temp;
                break;
            }
        }
        //在用户列表删除要下线的用户
        LocalData.userlist.remove(userModel);
        respModel.setUsers(LocalData.userlist);
        respModel.setGroups(LocalData.grouplist);
    }

    @Override
    public void addGroup(ReqModel reqModel, RespModel respModel) {
        respModel.setMsg(reqModel.getNickname() + ":创建了群 ：" + reqModel.getGroupName());
        //    把创建者加入到群组的成员列表中
        UserModel self = new UserModel(reqModel.getUid(), reqModel.getNickname());
        List<UserModel> users = new ArrayList<>();
        users.add(self);
        //此处 开源项目设计的是 UID由客户端创建 GroupID由服务端创建。思考 一般来说id都是服务端创建的
        String groupId = "group_" + reqModel.getUid() + "_" + reqModel.getGroupName();
        GroupModel groupModel = new GroupModel(groupId, reqModel.getGroupName(), users);
        LocalData.grouplist.add(groupModel);
        respModel.setGroups(LocalData.grouplist);
    }

    @Override
    public void joinGroup(ReqModel reqModel, RespModel respModel) {
        respModel.setMsg(reqModel.getNickname() + ":加入了群 ：" + reqModel.getGroupName());
        for (GroupModel groupModel : LocalData.grouplist) {
            if (groupModel.getId().equals(reqModel.getGroupId())) {
                UserModel self = new UserModel(reqModel.getUid(), reqModel.getNickname());
                groupModel.getUsers().add(self);
            }
        }
        respModel.setGroups(LocalData.grouplist);
    }

    //发送群组消息
    @Override
    public void sendGroupMsg(ReqModel reqModel, RespModel respModel) {
        respModel.setMsg(reqModel.getMsg());
        respModel.setGroupId(reqModel.getGroupId());
        respModel.setStatus(1);
    }

    //发送一对一消息
    @Override
    public void sendPrivateMsg(ReqModel reqModel, RespModel respModel) {
        respModel.setMsg(reqModel.getMsg());
        respModel.setBridge(reqModel.getBridge());
        respModel.setGroupId("");
        respModel.setStatus(1);
    }
}
